import pandas as pd
import holoviews as hv
from schimpy import schism_mesh
smesh = schism_mesh.read_mesh('../tests/data/m1_hello_schism/hgrid.gr3')
dfelems = pd.DataFrame(smesh.elems,columns=[0,1,2])
dfelems
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 0 | 2 | 1 |
| 1 | 1 | 4 | 3 |
| 2 | 2 | 5 | 4 |
| 3 | 3 | 7 | 6 |
| 4 | 4 | 8 | 7 |
| ... | ... | ... | ... |
| 4631 | 2630 | 2631 | 2626 |
| 4632 | 2631 | 2632 | 2627 |
| 4633 | 2633 | 2634 | 2630 |
| 4634 | 2634 | 2635 | 2631 |
| 4635 | 2636 | 2637 | 2634 |
4636 rows × 3 columns
replace 'z' with negative values for depth
dfnodes = pd.DataFrame(smesh.nodes, columns=['x','y','z'])
dfnodes['depth'] = -dfnodes.z
dfnodes = dfnodes.drop(columns=['z'])
dfnodes
| x | y | depth | |
|---|---|---|---|
| 0 | 56000.00 | -10400.00 | -0.500000 |
| 1 | 55831.58 | -10400.00 | -0.500000 |
| 2 | 56000.00 | -10350.00 | -0.944444 |
| 3 | 55663.16 | -10400.00 | -0.500000 |
| 4 | 55831.58 | -10348.07 | -1.023290 |
| ... | ... | ... | ... |
| 2634 | 55831.58 | 10348.07 | -1.023290 |
| 2635 | 55663.16 | 10400.00 | -0.500000 |
| 2636 | 56000.00 | 10350.00 | -0.944444 |
| 2637 | 55831.58 | 10400.00 | -0.500000 |
| 2638 | 56000.00 | 10400.00 | -0.500000 |
2639 rows × 3 columns
Adapted from https://anaconda.org/philippjfr/brain/notebook?version=2017.05.04.1924
The code below allows for the simplices already defined by elems to be used instead of doing a Delaunay triangulation (used from scipy as a way to calculate the simplices)
# uncomment and the run install script below if plotly is not available
#!conda install -y -c conda-forge plotly
hv.extension('plotly')
import param
class TriSurface(hv.TriSurface):
simplices = param.Array()
class TriSurfacePlot(hv.plotting.plotly.TriSurfacePlot):
style_opts = ['cmap', 'plot_edges']
def get_data(self, element, ranges, style, **kwargs):
if element.simplices is None:
return super(TriSurfacePlot, self).get_data(element, ranges, style, **kwargs)
x, y, z = (element.dimension_values(i) for i in range(3))
simplices = element.simplices
return [dict(x=x, y=y, z=z, simplices=simplices)]
hv.Store.register({TriSurface: TriSurfacePlot}, 'plotly')
tris = TriSurface(dfnodes, simplices = dfelems.values).opts(width=800, height=800, cmap='blues_r')
tris.opts(plot_edges=False, colorbar=True)
Save mesh as html to embed in docs
hv.save(tris,'mesh_surface_colored_by_depth.html')
Alternate way of displaying the z (depth values) with color
hv.extension('bokeh')
from holoviews.operation import datashader
trimesh = hv.TriMesh((dfelems.values, hv.Points(dfnodes,vdims='depth'))).opts(cmap='fire',node_alpha=0,edge_color='z',filled=True)
img = datashader.rasterize(trimesh).opts(cmap='rainbow4', colorbar=True, tools=['hover'], width=800)
img
hv.save(img,'mesh_colored_by_depth.html')